package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProviderBean;
import com.project.mapper.IProviderMapper;
import com.project.service.IProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 供应商实现类
 */

@Service
@Transactional
public class ProviderServiceImpl implements IProviderService {
    @Autowired
    private IProviderMapper providerMapper;
    @Override
    public IPage<ProviderBean> cutByItem(Integer pageNO, String providerName, String manageRange) {
        QueryWrapper<ProviderBean> qw=new QueryWrapper<>();
        if (providerName !=null && providerName.length() !=0){
            qw.like("p_providerName",providerName);
        }
        if (manageRange !=null && manageRange.length() !=0){
            qw.like("p_manageRange",manageRange);
        }
        return providerMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public ProviderBean findByproviderId(Integer providerId) {
        return providerMapper.selectById(providerId);
    }

    @Override
    public void updateNamePhoneById(Integer providerId, String name, String phone) {
       ProviderBean providerObj=providerMapper.selectById(providerId);
       providerObj.setPrincipalName(name);
       providerObj.setPhone(phone);
        providerMapper.updateById(providerObj);
    }


    @Override
    public void addProvider(ProviderBean provider) {
        QueryWrapper<ProviderBean> qw=new QueryWrapper<>();
        qw.eq("p_providerName",provider.getProviderName());
         ProviderBean providerObj=providerMapper.selectOne(qw);
         if (providerObj ==null){
             providerMapper.insert(provider);
         }

    }

    @Override
    public void addProviderList(List<ProviderBean> providerList) {
        for (ProviderBean provider: providerList) {
            QueryWrapper<ProviderBean> qw=new QueryWrapper<>();
            qw.eq("p_providerName",provider.getProviderName());
            ProviderBean providerObj=providerMapper.selectOne(qw);
            if (providerObj ==null){
                providerMapper.insert(provider);
            }
        }
    }

    @Override
    public List<ProviderBean> findAllProvider() {
        return providerMapper.selectList(null);
    }
}
